package com.zr.subclass.controller;

import com.zr.subclass.model.SubClassAddVo;
import com.zr.subclass.model.SubClassSelectVo;
import com.zr.subclass.model.SubClassUpdateStatusVo;
import com.zr.subclass.model.SubClassUpdateVo;
import com.zr.subclass.service.SubClassService;
import com.zr.util.ResultBuilderVo;
import com.zr.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by Lenovo on 2019/12/22.
 */
@RestController
@CrossOrigin//支持跨越
public class SubClassController {
    @Autowired
    private SubClassService planClassService;

    /*新增计划大类*/
    @PostMapping("subClass/add")
    public ResultVo add(@RequestBody @Valid SubClassAddVo planClassAddVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return planClassService.addPlanClass(planClassAddVo);
    }
    //修改计划大类
    @PostMapping("subClass/update")
    public ResultVo update(@RequestBody @Valid SubClassUpdateVo planClassUpdateVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return planClassService.updatePlanClass(planClassUpdateVo);
    }
    //修改计划大类启用禁用状态
    @PostMapping("subClass/updateStatus")
    public ResultVo updateStatus(@RequestBody @Valid SubClassUpdateStatusVo planClassUpdateStatusVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return planClassService.updateStatus(planClassUpdateStatusVo);
    }
    //查看
    @GetMapping("subClass/see")
    public ResultVo see(Integer id){
        if (id==null){
            return ResultBuilderVo.error("查看时id不能为空");
        }
        return planClassService.see(id);
    }
    /*分页*/
    @PostMapping("subClass/queryPage")
    public ResultVo queryPage(@RequestBody SubClassSelectVo planClassSelectVo){

        return planClassService.queryPage(planClassSelectVo);
    }

}
